#include "GUI.h"
using namespace std;

GUI::~GUI ( ) {
  std::cout<<"GUI deleted \n";
  SDL_Quit();
  delete camera;
}

GUI::GUI ( ) {
  screenXmin = 0U;
  screenYmin = 0U;
  screenXmax = 640U;
  screenYmax = 400U;
  XMIN = MyFloat(0.0F);
  YMIN = MyFloat(0.0F);
  XMAX = MyFloat(500.0F);
  YMAX = 150.0F;
  cout<<"GUI ctor executed"<<endl;

  camera = Camera::instance();

  SDL_Init(SDL_INIT_VIDEO);
  SDL_WM_SetCaption("Mon premier programme OpenGL !",NULL);
  SDL_SetVideoMode(screenXmax, screenYmin, 32, SDL_OPENGL);
  glMatrixMode( GL_PROJECTION );
  gluOrtho2D(0,screenXmax, 0, screenYmax);
  glLoadIdentity();
  SDL_EnableKeyRepeat(10,10);


}

// Methods
//
bool GUI::getUserActions (){ //interfacewith keyboard or mouse
  bool continuer = true;
  SDL_WaitEvent(&event);
  switch (event.type)
  {
  case SDL_QUIT:
    continuer = false;
    break;
  case SDL_KEYDOWN:
    if (event.key.keysym.sym==SDLK_ESCAPE)
      continuer = false;
    break;
  default:
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3ub(255,0,0);
    glVertex2d(-0.75,-0.75);
    glColor3ub(0,255,0);
    glVertex2d(0,1);
    glColor3ub(0,0,255);
    glVertex2d(0.75,-0.75);
    glEnd();
    glBegin(GL_LINES);
    glColor3ub(0,0,255);
    glVertex2i(0,0);
    glVertex2i(1,0);
    glEnd();
    glFlush();
    SDL_GL_SwapBuffers();
    usleep(20);
    glTranslatef(1E-2,1E-2,0);

    break;
  }


  //std::cin>>continuer;
  return continuer;
}

// Accessor methods
//

GUI* GUI::instance_ptr = 0;
// Other methods
//

GUI* GUI::instance ( )
{
  if (instance_ptr == 0)
    instance_ptr  = new GUI();
  return instance_ptr;
}
